<template>
  <DataList 
  ref="datalist"
  :excelHeader="excelHeader" 
  :cate="cate" :cols="cols"
  :name="name" 
  skipExcelNumber="2" 
  />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {get在校生信息By班级,get在校生信息ByXSXH} from "@/api/在校生信息"
import {get班级,get在校生信息} from "@/api/在校生信息"
import linq from "linq"
import { addCxxs, deleteCxxs, updateCxxs, getCxxs ,bulkAddCxxs} from "@/api/辍学学生明细表.js";
import {findComponent} from "@/utils/ui";
import { reject } from 'q';
export default {
  components: {
    DataList
  },
   mounted() {
    console.log(this.$root)
  },
  data() {
    return {
      cate: "A10-2-1辍学学生明细表",
      name:'10.2.1 辍学学生明细表',
      学生:[],
      excelHeader:{B3:"班级代码",C3:"班级名称",D3:"学号",E3:"姓名",F3:"辍学原因",G3:"辍学日期（年月）",},
      cols: [
       
        { label: "班级代码", prop: "BJDM" },
        { label: "班级名称(全称)", prop: "BJMC" },
        { label: "学号", prop: "XSXH" },
        { label: "姓名", prop: "XM" },
        { label: "辍学原因", prop: "XZ_CXYY" },
        { label: "辍学日期(年月)", prop: "CXRQ" },
        { label: "性别", prop: "XZ_XB" },
        { label: "专业代码", prop: "ZYDM" },
        { label: "专业名称(全称)", prop: "ZYMC" },
        { label: "专业方向代码", prop: "ZYFXDM" },
        { label: "专业方向名称(全称)", prop: "ZYFXMC" }

            ]
          }              
      
      },

    methods: {
      getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]= "合计";
        sums[6]= "班级数:"+linq.from(list).distinct("$.BJMC").count();
        sums[8]= "学生数:"+linq.from(list).distinct("$.XSXH").count();
        sums[13]= "专业数:"+linq.from(list).distinct("$.ZYMC").count();
        sums[15]= "方向数:"+linq.from(list).distinct("$.ZYFXMC").count();
        sums[17]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[17] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[17] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        
       return sums;
      //
    },
  
  //导入检查方法
  check(array,list) {

    return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var 在校生 = await get在校生信息(user.XXID,user.CJPC)
          //本身重复性检查
          var duplicates =  linq.from(array).groupBy("$.XSXH").where("$.count()>1").toArray();
          duplicates.forEach(e=>{
            linq.from(array).where(t=>t.XSXH==e.key()).forEach(t=>t.error="录入数据存在相同的学号");
          })
          //是否已经存在数据
        
          var err =  linq.from(array).select("$.XSXH").intersect(linq.from(list).select("$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在对应的学号");
          
          //是否已经存在相同
          
          err =  linq.from(array).select("$.XSXH").except(linq.from(在校生).select("$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"学生信息表不存在对应的学号");
          //把错误的班级代码和班级名称，学生姓名替代掉
          err =  linq.from(array).select("$.BJDM+','+$.XSXH").except(linq.from(在校生).select("$.BJDM+','+$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.BJDM+','+e.XSXH) !==-1).
          forEach(e=>{
            var d = linq.from(在校生).firstOrDefault(item=>item.XSXH == e.XSXH);
            if(d){
              e.BJDM = d.BJDM;
              e.BJMC = d.BJMC;
              e.XM = d.XM;
              e.XZ_XB = d.XZ_XB;
              e.ZYDM = d.ZYDM;
              e.ZYMC = d.ZYMC;
              e.ZYFXDM = d.ZYFXDM;
              e.ZYFXMC = d.ZYFXMC;
              
            }
          });
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },
    
    async  onChange(model, field, value) {
      
       if(field == "XSXH"){
         if(value == "error"){
           console.log("error")
          model.XSXH="";
          model.XM="";
          model.XZ_XB="";
          model.ZYDM="";
          model.ZYMC="";
          model.ZYFXDM="";
          model.ZYFXMC="";
          model.BJDM="";
          model.BJMC="";
         }else{
         var d1 =  await get在校生信息ByXSXH(this.$root.user.XXID,this.$root.user.CJPC,value);
        
           console.log("udpate ")
          model.XSXH = d1[0].XSXH;
          model.XM = d1[0].XM;
          model.XZ_XB = d1[0].XZ_XB;
          model.ZYDM = d1[0].ZYDM;
          model.ZYMC = d1[0].ZYMC;
          model.ZYFXDM = d1[0].ZYFXDM;
          model.ZYFXMC = d1[0].ZYFXMC;
          model.BJDM = d1[0].BJDM;
          model.BJMC = d1[0].BJMC;
         
         }
       }
      //
    },
    //别的页面不用复制此方法
    
  
 
    
    async fetchData() {
      //
      var datas = await getCxxs();
      return datas;
    },
   add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.XSXH ==  model.XSXH);
          
          if(first)
          return "已经存在对应学号的学生"
      //
    },
     onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      try{     
          console.log(JSON.stringify(temp));
          var model = await addCxxs(temp);
          console.log(model)
          resolve(model);
          }catch(e){
        reject(e);
      }
      });
      
    },
    async  bulkAdd(array){
       await  bulkAddCxxs({Cxxs:array});
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateCxxs(temp);
          console.log(model)
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteCxxs(temp);
          resolve();
      })
    }
       }
  };
  

</script>
<style>
</style>